package com.taijihuabao.modules.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.ReimbursementEntity;
import com.taijihuabao.modules.mapper.ReimbursementFromMapper;
import com.taijihuabao.modules.model.reimbursement.ReimbursementExportDTO;
import com.taijihuabao.modules.model.reimbursement.ReimbursementExportVO;
import com.taijihuabao.modules.service.ReimbursementService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @title: ReimbursementImpl
 * @Author luoxianan
 * @Date: 2022/1/12 10:16
 * @Version 1.0
 */
@Service
public class ReimbursementImpl implements ReimbursementService {
    /**
     * 第一周
     */
    private static final int FIRST_WEEK = 1;
    /**
     * 第二周
     */
    private static final int SECOND_WEEK = 2;
    /**
     * 第三周
     */
    private static  final int THIRD_WEEK = 3;
    /**
     * 第四周
     */
    private static  final int FOURTH_WEEK = 4;
    /**
     * 第五周
     */
    private static  final int FIFTH_WEEK = 5;
    /**
     * 第一周第一天
     */
    private static final int FIRST_WEEK_FIRST_DAY = 1;
    /**
     * 第一周最后一天
     */
    private static final int FIRST_WEEK_LAST_DAY = 7;
    /**
     * 第二周第一天
     */
    private static final int SECOND_WEEK_FIRST_DAY = 8;
    /**
     * 第二周最后一天
     */
    private static final int SECOND_WEEK_LAST_DAY = 14;
    /**
     * 第三周第一天
     */
    private static  final int THIRD_WEEK_FIRST_DAY = 15;
    /**
     * 第三周最后一天
     */
    private static  final int THIRD_WEEK_LAST_DAY = 21;
    /**
     * 第四周第一天
     */
    private static  final int FOURTH_WEEK_FIRST_DAY = 22;
    /**
     * 第四周最后一天
     */
    private static  final int FOURTH_WEEK_LAST_DAY = 28;
    /**
     * 第五周第一天
     */
    private static  final int FIFTH_WEEK_FIRST_DAY = 29;
    /**
     * 第五周最后一天
     */
    private static  final int FIFTH_WEEK_LAST_DAY = 31;
    @Autowired
    private ReimbursementFromMapper reimbursementFromMapper;

    @Override
    public void insertExpense(ReimbursementEntity reimbursementEntity) {
        reimbursementFromMapper.insertExpense(reimbursementEntity);
    }

    @Override
    public void selectAll(HttpServletResponse response, ReimbursementExportDTO reimbursementExportDTO) throws IOException {
        List<ReimbursementEntity> reimbursementEntities = reimbursementFromMapper.selectAll();
        responseSet(reimbursementEntities,response,reimbursementExportDTO);
    }

    @Override
    public void selectByMonth(HttpServletResponse response, ReimbursementExportDTO reimbursementExportDTO) throws IOException {
        List<ReimbursementEntity> reimbursementEntities = reimbursementFromMapper.selectByMonth(reimbursementExportDTO.getMonth());
        if (CollectionUtils.isEmpty(reimbursementEntities)){
            throw new ServiceException(ApplicationResponseCode.MONTH_NOT_DATE);
        }
        responseSet(reimbursementEntities,response,reimbursementExportDTO);
    }

    @Override
    public void selectByWeek(HttpServletResponse response, ReimbursementExportDTO reimbursementExportDTO) throws IOException {
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Integer week = reimbursementExportDTO.getWeek();
        instance.set(Calendar.MONTH,reimbursementExportDTO.getMonth()-1);
        instance.set(Calendar.YEAR,reimbursementExportDTO.getYear());
        String beginTime = null;
        String endTime = null;
        switch (week){
            case FIRST_WEEK:
                instance.set(Calendar.DAY_OF_MONTH,FIRST_WEEK_FIRST_DAY);
                beginTime = simpleDateFormat.format(instance.getTime());
                instance.set(Calendar.DAY_OF_MONTH,FIRST_WEEK_LAST_DAY);
                endTime = simpleDateFormat.format(instance.getTime());
                break;
            case SECOND_WEEK:
                instance.set(Calendar.DAY_OF_MONTH,SECOND_WEEK_FIRST_DAY);
                beginTime = simpleDateFormat.format(instance.getTime());
                instance.set(Calendar.DAY_OF_MONTH,SECOND_WEEK_LAST_DAY);
                endTime = simpleDateFormat.format(instance.getTime());
                break;
            case THIRD_WEEK:
                instance.set(Calendar.DAY_OF_MONTH,THIRD_WEEK_FIRST_DAY);
                beginTime = simpleDateFormat.format(instance.getTime());
                instance.set(Calendar.DAY_OF_MONTH,THIRD_WEEK_LAST_DAY);
                endTime = simpleDateFormat.format(instance.getTime());
                break;
            case FOURTH_WEEK:
                instance.set(Calendar.DAY_OF_MONTH,FOURTH_WEEK_FIRST_DAY);
                beginTime = simpleDateFormat.format(instance.getTime());
                instance.set(Calendar.DAY_OF_MONTH,FOURTH_WEEK_LAST_DAY);
                endTime = simpleDateFormat.format(instance.getTime());
                break;
            case FIFTH_WEEK:
                instance.set(Calendar.DAY_OF_MONTH,FIFTH_WEEK_FIRST_DAY);
                beginTime = simpleDateFormat.format(instance.getTime());
                instance.set(Calendar.DAY_OF_MONTH,FIFTH_WEEK_LAST_DAY);
                endTime = simpleDateFormat.format(instance.getTime());
                break;
            default:
                new ServiceException(ApplicationResponseCode.WEEK_NOT_DATE);
        }
        List<ReimbursementEntity> reimbursementEntities = reimbursementFromMapper.selectByWeek(reimbursementExportDTO.getMonth(), beginTime, endTime);
        if (CollectionUtils.isEmpty(reimbursementEntities)){
            new ServiceException(ApplicationResponseCode.WEEK_NOT_DATE);
        }
        responseSet(reimbursementEntities,response,reimbursementExportDTO);
    }

    private List<ReimbursementEntity> responseSet(List<ReimbursementEntity> list, HttpServletResponse response, ReimbursementExportDTO reimbursementExportDTO) throws IOException{
        //头部策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = new String(reimbursementExportDTO.getFileName().getBytes("ISO-8859-1"), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        // 这里需要设置不关闭流
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream(), ReimbursementExportVO.class).autoCloseStream(Boolean.FALSE)
                .registerWriteHandler(horizontalCellStyleStrategy).sheet(reimbursementExportDTO.getSheetName())
                .doWrite(list);
        return list;
    }
}
